package com.xposed.aliproxy.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.xposed.aliproxy.MainActivity;
import com.xposed.aliproxy.db.AliTradeModel;
import com.xposed.aliproxy.qrcode.GlobalInstance;
import com.xposed.aliproxy.socket.SocketManager;
import com.xposed.aliproxy.thread.SocketHeartBeatThread;
import com.xposed.aliproxy.thread.SocketReadThread;
import com.xposed.aliproxy.thread.SocketWriteThread;
import com.xposed.aliproxy.thread.UploadAliTradeThread;
import com.xposed.aliproxy.utils.DateUtil;
import com.xposed.aliproxy.utils.ParamConfig;
import com.xposed.aliproxy.utils.ToastUtil;
import com.xposed.aliproxy.utils.XposeUtil;

/**
 * Ali登录或退出广播监听
 */
public class AliLogInOrOutReceiver extends BroadcastReceiver {
    private int loginCount;
    private Context context;
    private SocketHeartBeatThread heartBeatThread;
    private SocketReadThread readThread;
    private SocketWriteThread writeThread;
    private Object lockKey = new Object();

    public AliLogInOrOutReceiver(Context context) {
        this.context = context;
        this.loginCount = 1;
    }

    @Override
    public synchronized void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals("com.abcd.xpouseali.action.aliloginid")) {
            if (this.loginCount == 1) {
                this.loginCount++;
                action = intent.getStringExtra("local_realaliid");
                ParamConfig.getInstance(this.context).putString("local_realaliid", action);
                if (checkAccount(ParamConfig.getInstance(this.context).getString("aliAccount", ""), action)) {
                    synchronized (this.lockKey) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                if (AliLogInOrOutReceiver.this.heartBeatThread == null) {
                                    AliLogInOrOutReceiver.this.heartBeatThread = new SocketHeartBeatThread(AliLogInOrOutReceiver.this.context);
                                }
                                if (AliLogInOrOutReceiver.this.readThread == null) {
                                    AliLogInOrOutReceiver.this.readThread = new SocketReadThread(AliLogInOrOutReceiver.this.context);
                                }
                                if (AliLogInOrOutReceiver.this.writeThread == null) {
                                    AliLogInOrOutReceiver.this.writeThread = new SocketWriteThread(AliLogInOrOutReceiver.this.context);
                                }
                                if (!AliLogInOrOutReceiver.this.heartBeatThread.f1793b) {
                                    AliLogInOrOutReceiver.this.heartBeatThread.start();
                                }
                                if (!AliLogInOrOutReceiver.this.readThread.f1797b) {
                                    AliLogInOrOutReceiver.this.readThread.start();
                                }
                                if (!AliLogInOrOutReceiver.this.writeThread.f1801b) {
                                    AliLogInOrOutReceiver.this.writeThread.start();
                                }
                            }
                        }).start();
                    }
                } else {
                    this.loginCount = 1;
                    XposeUtil.sendBroadcaseMsg(this.context, "支付宝账号不匹配");
                    this.context.startActivity(new Intent(this.context, MainActivity.class));
                }
            }
        } else if (action.equals("com.abcd.xpouseali.action.aliloginsuccess")) {
            this.loginCount++;
            XposeUtil.sendBroadcaseMsg(this.context, "支付宝绑定成功");
            GlobalInstance.getInstance().f1765d = true;
        } else if (action.equals("com.abcd.xpouseali.action.aliloginfailed")) {
            this.loginCount = 1;
            this.context.startActivity(new Intent(this.context, MainActivity.class));
        } else if (action.equals("com.abcd.xpouseali.action.alilogout")) {
            this.loginCount = 1;
            GlobalInstance.getInstance().f1765d = false;
            GlobalInstance.getInstance().f1764c = true;
            this.heartBeatThread.f1792a = true;
            this.readThread.f1796a = true;
            this.writeThread.f1800a = true;
            this.heartBeatThread = null;
            this.readThread = null;
            this.writeThread = null;
            SocketManager.m2950b();
            GlobalInstance.getInstance().stringArrayList.clear();
            GlobalInstance.getInstance().qrCodeModelArrayList.clear();
            AliTradeModel aliTradeModel = new AliTradeModel();
            aliTradeModel.setAliBalance(GlobalInstance.getInstance().f1769h);
            aliTradeModel.setTradeNo(DateUtil.formatData("yyyyMMddHHmmssSSS"));
            aliTradeModel.setName("name");
            aliTradeModel.setRemark("remark");
            aliTradeModel.setAmount("0");
            aliTradeModel.setDate(DateUtil.formatData("yyyy-MM-dd HH:mm"));
            UploadAliTradeThread uploadAliTradeThread = new UploadAliTradeThread(this.context, aliTradeModel);
            if (!intent.getBooleanExtra("finish", false)) {
                this.context.startActivity(new Intent(this.context, MainActivity.class));
                ToastUtil.showToast(this.context, "支付宝解绑");
            }
        }
    }

    private boolean checkAccount(String str, String str2) {
        if (str2.contains("@")) {
            return str.equals(str2);
        }
        String substring = str.substring(0, 3);
        str = str.substring(str.length() - 2, str.length());
        String substring2 = str2.substring(0, 3);
        str2 = str2.substring(str2.length() - 2, str2.length());
        if (substring.equals(substring2) && str.equals(str2)) {
            return true;
        }
        return false;
    }
}
